# -*- coding: utf-8 -*-
"""
Created on Sat Sep 17 10:25:15 2022

@author: 123
"""

new_df_risk1=new_df_outlier.query("Risk_Flag=='1' & is_outlier=='1'")

new_df_risk1=new_df_outlier.query('Risk_Flag==1 & is_outlier==1')

new_df_not_outlier=new_df_outlier.query('is_outlier==1')
grouped_Risk_Flag=new_df_not_outlier['Risk_Flag'].groupby(new_df_not_outlier['Risk_Flag'])
print(grouped_Risk_Flag.count())

# 不均衡数据集 28144

#层次聚类

from sklearn.preprocessing import normalize
data_scaled = normalize(new_df_risk1)

#data_scaled.dtype=np.float16

#转化为数组
data_scaled = pd.DataFrame(data_scaled, columns=new_df_risk1.columns).to_numpy()

#array_name.dtype = np.uint8




#import scipy.cluster.hierarchy as shc
#import matplotlib.pyplot as plt




import pandas as pd
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

#df_features = pd.read_csv(r'C:\预处理后数据.csv',encoding='gbk') # 读入数据

#https://blog.csdn.net/lovenankai/article/details/99966078  SSE判断 

#'利用SSE选择k'
SSE = []  # 存放每次结果的误差平方和
for k in range(1,10):
    estimator = KMeans(n_clusters=k,init='k-means++', n_init=10, max_iter=300,random_state=0)  # 构造聚类器
    estimator.fit(data_scaled)
    SSE.append(estimator.inertia_)
X = range(1,10)
plt.xlabel('k')
plt.ylabel('SSE')
plt.plot(X,SSE,'o-')
plt.show()




#k值 取3 

from sklearn import metrics
model_kmeans=KMeans(n_clusters=3,init='k-means++', n_init=10, max_iter=300,random_state=0)  #建立模型对象
model_kmeans.fit(data_scaled)    #训练聚类模型
y_pre_kmeans=model_kmeans.predict(data_scaled)   #预测聚类模型

np.size(y_pre_kmeans)

#new_df_risk1 分三类 + y_pre 值 

new_df_risk1_reset=new_df_risk1.reset_index(drop=True)

#列重命名
y_pre_kmeans_df=pd.DataFrame(y_pre_kmeans)
p_col=['kmeans']
y_pre_kmeans_df.columns=p_col
new_df_risk1_kmeans=pd.concat([new_df_risk1_reset, y_pre_kmeans_df], join="inner", axis=1)


